﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;
using IPMS.Common;

public partial class SysUserGroupsUpdate : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        try
        {
            if (Session["Culture"] != null)
            {
                strCulture = Session["Culture"].ToString();
            }
        }
        catch (NullReferenceException)
        {
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!Page.IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.UserSettings.ToString() + " | " + ISitemap.UserGroupUpdate;
            lblGroupId.Text = Request["groupid"].ToString();

            GroupsDB objDB = new GroupsDB();
            DataRow dr = objDB.getUserGroupById(lblGroupId.Text);
            objDB.CloseDB();

            if (dr != null)
            {
                txtGroupName.Text = objDB.GetValue(dr, "GroupName");
                txtRemark.Text = objDB.GetValue(dr, "Remark");
            }
        }
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnSave)
        {
            GroupsDB objDB = new GroupsDB();

            if (objDB.Update(lblGroupId.Text, txtGroupName.Text, txtRemark.Text) == 0)
            {
                new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_usergroup_manage, "Update User Group Success! User Group: " + txtGroupName.Text);
                Jscript.Alert("Update User Group Success!", Page);
            }
            else
            {
                new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_usergroup_manage, "Update User Group Failure! User Group: " + txtGroupName.Text);
                objDB.CloseDB();
                Jscript.Alert("Update User Group Failure!", Page);
                lblError.Text = objDB.ErrorMessage;
            }

            objDB.CloseDB();
        }
        else
        {
            Response.Redirect("SysUserGroups.aspx");
        }
    }
}
